<?php
	require_once("Database.php");

	class Post{
		private $id=0;
		private $announcement=0;
		private $user=0;
		private $content="";
		private $updateDate="";
		private $saveDate="";
		private $result="";
		
		public function setId($id){
			$this->id = $id;
		}
		
		public function setAnnouncement($announcementId){
			$this->announcement = $announcementId;
		}
		
		public function setUser($userId){
			$this->user = $userId;
		}
		
		public function setContent($content){
			$this->content = $content;
		}
		
		public function setUpdateDate($update){
			$this->updateDate = $update;
		}
		
		public function setSaveDate($save){
			$this->saveDate = $save;
		}
		
		public function getId(){
			return $this->id;
		}
		
		public function getAnnouncement(){
			return $this->announcement;
		}
		
		public function getUser(){
			return $this->user;
		}
		
		public function getContent(){
			return $this->content;
		}
		
		public function getUpdateDate(){
			return $this->updateDate;
		}
		
		public function getSaveDate(){
			return $this->saveDate;
		}
		
		public function setValues($ad, $user, $content){
			$this->announcement = $ad;
			$this->user = $user;
			$this->content = $content;
		}
		
		public function findById($id){
			$db = new Database("localhost", "root", "");
			$db->connect("emprego");
			$db->executeQuery("SELECT * FROM posts WHERE id='$id'");
			
			if($line = $db->getResultLine(MYSQL_ASSOC)){
				$this->setId($line["id"]);
				$this->setValues($line["announcement"], $line["candidate"], $line["content"]);
				$this->setUpdateDate($line["update_date"]);
				$this->setSaveDate($line["save_date"]);
				$db->disconnect();
				return TRUE;
			}else{
				$db->disconnect();
				return FALSE;
			}
		}
		
		public function findByAnnouncement($ad){
			if($this->result == ""){
				$db = new Database("localhost", "root", "");
				$db->connect("emprego");
				$db->executeQuery("SELECT * FROM posts WHERE announcement='$ad'");
				$this->result = $db->getResult();
				$db->disconnect();
			}
			if($line = mysql_fetch_array($this->result, MYSQL_ASSOC)){
				$this->setId($line["id"]);
				$this->setValues($line["announcement"], $line["candidate"], $line["content"]);
				$this->setUpdateDate($line["update_date"]);
				$this->setSaveDate($line["save_date"]);
				return TRUE;					
			}else{
				$this->result = "";
				return FALSE;
			}
		}
		
		public function save(){
			$db = new Database("localhost", "root", "");
			$db->connect("emprego");
			
			$db->executeQuery("SELECT id FROM posts WHERE id='$this->id'");
			if($line = $db->getResultLine(MYSQL_ASSOC)){
				$db->executeQuery("UPDATE posts SET announcement='$this->announcement', candidate='$this->user', content='$this->content',
									update_date= now()");
				return 1;
			}else{
				$db->executeQuery("INSERT INTO posts(announcement, candidate, content, update_date, save_date)
									VALUES('$this->announcement', '$this->user', '$this->content', now(), now())");
				return 2;
			}
		}
	}
?>